<template>
  <div class="ele-body aging">
    <el-card shadow="never" class="topCard">
      <div >及时关单率</div>
      <div >目标：了解并跟踪某要求的期限内成功完工的工单的比率。</div>
      <div >含义：在某个已定义工单的周期内完成的工单占所有的工单数量的比率。</div>
    </el-card>
    <el-card shadow="never">
      <CustomSelect label="工单类型：" @input="query" v-model="queryForm.work_order_type" :selectOptions="options1"/>
    </el-card>
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form :model="queryForm" ref="form" class="ele-form-search" label-width="auto" @keyup.enter.native="query"
               @submit.native.prevent>
        <el-row :gutter="15" type="flex" style="flex-wrap: wrap">
          <el-col :xl="6" :lg="8" :md="12">
            <el-form-item label="选择时间：" prop="date">
              <CustomDate :type.sync="queryForm.type" v-model="queryForm.date"/>
            </el-form-item>
          </el-col>
          <el-col :xl="6" :lg="8" :md="12">
            <el-form-item label="部门：" prop="department">
              <el-select v-model="queryForm.dept_id" placeholder="请选择部门" style="width: 100%">
                <el-option
                  v-for="item in options2"
                  :key="item.value"
                  :label="item.name"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xl="6" :lg="8" :md="12">
            <div class="ele-form-actions">
              <el-button :loading="loading" class="ele-btn-icon" icon="el-icon-search" type="primary" @click="query">
                查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <el-card class="ele-body" shadow="never">
      <div class="content">
        <Echarts4 v-loading="loading" :data="lineChartData"/>
      </div>
    </el-card>
  </div>
</template>
<script>
import CustomDate from "./components/CustomDate.vue"
import Echarts4 from "./components/Echarts4.vue"
import CustomSelect from "@/views/reports/workorder-monitoring/components/CustomSelect.vue";

export default {
  components: {CustomDate, Echarts4, CustomSelect},
  data() {
    return {
      loading: false,
      queryForm: {
        department: null,
        date: [],
        work_order_type: null,
        type: null,
      },
      options1: [],
      options2: [],
      lineChartData: '',
    }
  },
  mounted() {
    this.query()
    this.getParams()
  },
  methods: {
    getParams() {
      this.$http.get('wos/params').then(res => {
        if (res.data.code === 0) {
          this.options1 = res.data.data.wo_types.map(item => {
            return {label: item, value: item}
          })
        } else {
          this.$message.error(res.data.msg);
        }
      })
      this.$http.get('dept/index').then(res => {
        if (res.data.code === 0) {
          this.options2 = res.data.data
        } else {
          this.$message.error(res.data.msg);
        }
      })
    },
    query() {
      let form = {...this.queryForm}
      delete form.date
      if (this.queryForm.date.length) {
        form.month_start = this.queryForm.date[0]
        form.month_end = this.queryForm.date[1]
      }
      this.handleQueryLineChart(form)
    },
    reset() {
      this.$refs.form.resetFields()
      this.queryForm.work_order_type = null
      this.query()
    },
    //获取折线图数据
    async handleQueryLineChart(form) {
      this.loading = true
      const res = await this.$http.get('/reports/close_work_order', {params: form})
      res.data.success ? this.lineChartData = res.data.data : this.$message.error(res.data.msg);
      this.loading = false
    }
    ,
  },
}
</script>

<style scoped lang="scss">
.aging {
  div:nth-child(2) {
    ::v-deep.el-card__body {
      padding-bottom: 0;
    }
  }
}
.topCard{
  ::v-deep div:not(:first-child){
    margin-top: 10px;
  }
  div:first-child{
    font-size: 16px;
    font-weight: 700;
    color: #262626
  }
}
</style>
